import CryptoJS from 'crypto-js'
import JSEncrypt from 'jsencrypt'
import localStorageService from './localStorage.service'
/**
 * EncryptService
 * 加密服务
 */
const merchantID = 'MD2023'
const userKey = '5d39980acc6e4d6f91b04720c3414ef6'
const key = '0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z'
const publicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlnix2KuaKMyFOj3ZGPYNpRdPEarj6u8zUvy0RPATyPv346+x+9RoqHXH9uBJTsbQ2cBmYi0oEkHsqLTebff6PAvTfHiyBKJe1khmWtbwVJRXfRN/hK3jbobREm/GXoJKV7VZCVL3fkND7Pfhkc9WkhH7h0//4dH3vB0ww8o5jGtMn2Rh92eGylyoBd8KcS0+j7mywHe17KS+++MTCkU3Hnh4Xx2We0v3RSb/lC8/SdIe++3TsbPeQ5ICTM2K01oGiqqZGZqD5xLhXWQW5e8Ib6oems/iY2t/B6CosDkeHo0b6XBGis7J/ggjz0SJDA1/NbCF/cIANLlk6/Qwqk1SVwIDAQAB'
// const aesKey = ''

/**
 * 获取时间戳
 */
const getTimesTamp = () => {
  let date = new Date()

  let fullYear = date.getFullYear()
  let month = date.getMonth()
  let day = date.getDate()
  let hours = date.getHours()
  let minutes = date.getMinutes()
  let seconds = date.getSeconds()
  let milliseconds = date.getMilliseconds()

  let timesTamp = Date.UTC(
    fullYear,
    month,
    day,
    hours,
    minutes,
    seconds,
    milliseconds
  )

  return timesTamp
}
/**
 * 生成16位AES - KEY
 */
const generateAESKey = () => {
  let k = key.split(',')
  let keyLength = k.length
  let m = 0
  let n = keyLength - 1
  let str = ''

  for (let index = 0; index < 16; index++) {
    let ran = Math.random() * (n - m) + m;
    str = str + key[parseInt(ran.toString())]
  }

  return str
}
/**
 * 
 * @param data 加密数据
 * @param key 16位KEY
 */
const AESEncrypt = (data, key) => {
  let k = CryptoJS.enc.Utf8.parse(key)
  let i = CryptoJS.enc.Utf8.parse(key)

  let encrypted = CryptoJS.AES.encrypt(data, k, {
    iv: i,
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })

  let encryptedHexStr = CryptoJS.enc.Hex.parse(encrypted.ciphertext.toString())
  let encryptedBase64Str = CryptoJS.enc.Base64.stringify(encryptedHexStr)

  return encryptedBase64Str
}
/**
 * AES数据解密
 * @param {*} data 要解密的数据
 * @param {*} key 16位AES_KEY
 */
const AESDecrypt = (data, key) => {
  let k = CryptoJS.enc.Utf8.parse(key)
  let i = CryptoJS.enc.Utf8.parse(key)

  let decrypted = CryptoJS.AES.decrypt(data, k, {
    iv: i,
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })

  let decryptedUtf8Str = decrypted.toString(CryptoJS.enc.Utf8)

  return decryptedUtf8Str
}
/**
 * RSA加密
 * @param {*} aesKey AES 加密数据
 */
const RSAEncrypt = (aesKey) => {
  let encrypt = new JSEncrypt()

  encrypt.setPublicKey(publicKey)

  let aeskey = encrypt.encrypt(aesKey)

  return aeskey
}
/**
 * URL 参数加密
 * @param {*} str 要加密的参数
 */
const UrlEncode = (str) => {
  return encodeURI(str)
}
/**
 * URL 参数解密
 * @param {*} str 要解密的参数
 */
const UrlDecode = (str) => {
  return decodeURI(str)
}
/**
 * MD5加密
 * @param {*} object 要加密的字符
 * @param {*} isLowerCase 是否小写
 */
const MD5Encryption = (object = '1234567890', isLowerCase = true) => {
  if (isLowerCase) {
    return (CryptoJS.MD5(object) + '').toLowerCase()
  } else {
    return (CryptoJS.MD5(object) + '').toUpperCase()
  }
}
/* 获取Md5加密sign值
 * @param {*} MerchantId 商户编号
 * @param {*} TimesTamp 时间戳
 * @param {*} Ip IP地址
 * @param {*} Mac MAC
 */
const getSign = (MerchantId, TimesTamp, Ip, Mac) => {
  let jsonData = `MerchantId=${MerchantId}&TimesTamp=${TimesTamp}&Ip=${Ip}&Mac=${Mac}${userKey}`

  let sign = CryptoJS.MD5(jsonData) + ''

  return sign.toUpperCase()
}
/**
 * 实体加密（RSA证书加密）
 * @param {*} model
 * @param {*} isEncryption
 */
const dataRSAEncryption = (model, isEncryption = false) => {
  let encryptionModel = {
    MerchantId: null,
    Data: null,
    EncryptKey: null
  }

  encryptionModel.MerchantId = merchantID
  let ran_aesKEY = generateAESKey()
  model.ip = '167.0.12.31'
  model.mac = '00-15-5D-7E-36-5B'
  model.isValid = true;
  model.version = '1.0.0'
  model.timesTamp = getTimesTamp() + ''
  if (localStorageService.getItem('Authorization')) {
    model.token = `Bearer ${localStorageService.getItem('Authorization')}`
  }
  else {
    model.token = ''
  }

  model.sign = null

  if (isEncryption) {
    //新版本
    model.Sign = getSign(
      encryptionModel.MerchantId,
      model.TimesTamp,
      model.Ip,
      model.Mac
    )
  }

  encryptionModel.Data = JSON.stringify(model)
  encryptionModel.EncryptKey = RSAEncrypt(ran_aesKEY)

  return encryptionModel
}
/**
 * 实体加密（明参）
 * @param {*} model 
 * @param {*} isEncryption 
 */
const dataEncryption = (model, isEncryption = true) => {
  model.merchantId = merchantID;
  model.ip = '167.0.12.31'
  model.mac = '00-15-5D-7E-36-5B'
  model.isValid = true
  model.version = '1.0.0'
  model.timesTamp = getTimesTamp() + ''
  if (localStorageService.getItem('Authorization')) {
    model.token = `Bearer ${localStorageService.getItem('Authorization')}`
  }
  else {
    model.token = ''
  }

  model.sign = null

  if (isEncryption) {
    //新版本
    model.sign = getSign(
      merchantID,
      model.timesTamp,
      model.ip,
      model.mac
    )
  }

  return model
}

export const _getTimesTamp = getTimesTamp
export const _generateAESKey = generateAESKey
export const _AESEncrypt = AESEncrypt
export const _AESDecrypt = AESDecrypt
export const _RSAEncrypt = RSAEncrypt
export const _UrlEncode = UrlEncode
export const _UrlDecode = UrlDecode
export const _MD5Encryption = MD5Encryption
export const _getSign = getSign
export const _dataRSAEncryption = dataRSAEncryption
export const _dataEncryption = dataEncryption